#include <iostream>

using namespace std;

int n = 13, m = 4;

int a[14][5];
int cnt[14]; // 借助cnt数组，找到每行的最后一个数
//0表示10，J表示11，Q表示12

//拿一张牌，直接拿出x堆的最后一张牌，cnt[x]--
void dfs(int x)
{
	if(x == 13) return;

	int t = a[x][cnt[x]];
	cnt[x]--;
	dfs(t);
}

int main()
{
	for(int i = 1; i <= 13; i++)
	{
		cnt[i] = 4;//初始化 cnt 数组
		for(int j = 1; j <= 4; j++)
		{
			char ch; cin >> ch;
			if(ch >= '2' && ch <= '9') a[i][j] = ch - '0';
			else if(ch == 'A') a[i][j] = 1;
			else if(ch == '0') a[i][j] = 10;
			else if(ch == 'J') a[i][j] = 11;
			else if(ch == 'Q') a[i][j] = 12;
			else a[i][j] = 13;
		}
	}

	for(int i = 1; i <= m; i++)
	{
		dfs(a[n][i]);
	}

	//统计对数
	int ret = 0;
	for(int i = 1; i <= n; i++)
	{
		if(cnt[i] == 0) ret++;
	}

	cout << ret << endl;

	return 0;
}

